<template>
  <div class="functionType">
    <div class="titleTop first">
      <h3>{{$t('position_management.cooper_position')}}：{{positionInfo.name}}</h3>
      <a-button type="primary" @click="recommedResume(true, {})" v-if="positionInfo.status === 'PUBLISHING' && positionInfo.vendorPartnerStatus === 'Y'">{{$t('position_management.add_resume')}}</a-button>
    </div>
    <a-tabs :activeKey="activeKey" @change="callback">
      <template slot="tabBarExtraContent">
        <a href="javascript:;" style="margin-right:24px" @click="backPage">{{$t('common.back')}}</a>
      </template>
      <a-tab-pane key="1" :tab="$t('navigation.position_editing')">
        <div class="content pt">
          <a-row :gutter="24" style="flex:1">
            <a-col :span="18">
              <!-- record.status === 'PUBLISHING' && record.vendorPartnerStatus === 'Y' -->
              <a-card style="margin-bottom: 12px" v-if="searchType === 'COMPANY' && positionInfo.status === 'PUBLISHING' && positionInfo.vendorPartnerStatus === 'Y'">
                <div class="distrube">
                  <div class="tagsList">
                    <a-tag style="margin-bottom: 4px" color="#108ee9" v-for="item in positionInfo.job_vendor_array" :key="item.id" closable @close="(e) => closeVendors(e, item.id)">
                      {{item.name}}
                    </a-tag>
                  </div>
                  <a-button type="dashed" class="addDistrub" @click="distributeResume(true, {})">
                    + 分配猎头
                  </a-button>
                </div>
              </a-card>
              <a-card :title="$t('position_management.positon_desc')">
                <div v-html="positionInfo.detail"></div>
              </a-card>
            </a-col>
            <a-col :span="6">
              <div>
                <a-card class="positionBasicInfo">
                  <template slot="title">
                    <div style="display: flex; justify-content: space-between">
                      <span>{{$t('navigation.post_release')}}</span>
                      <a-pagination simple :current="current" :pageSize="1" :total="max" @change="pageChange"/>
                    </div>
                  </template>
                  <template v-for="tmp in (positionTemp[0] && positionTemp[0].fields && positionTemp[0].fields.filter(item => item.visible))">
                    <div :key="tmp.id">
                      <h6>{{tmp.displayName}}</h6>
                      <p v-html="positionInfo['str' + tmp.key.charAt(0).toUpperCase() + tmp.key.slice(1)] !== undefined ? positionInfo['str' + tmp.key.charAt(0).toUpperCase() + tmp.key.slice(1)] :( (positionInfo[tmp.key] && positionInfo[tmp.key].name) || positionInfo[tmp.key])"></p>
                    </div>
                  </template>
                  <!-- <h6>部门</h6>
                  <p>{{positionInfo.strDepartment}}</p>
                  <h6>工作经验</h6>
                  <p>{{positionInfo.strWorkYears}}</p>
                  <h6>工作地点</h6>
                  <p>{{positionInfo.strProvince}}-{{positionInfo.strCity}}</p>
                  <h6>学历要求</h6>
                  <p>{{positionInfo.strDegree}}及以上</p>
                  <h6>行业类别</h6>
                  <p>{{positionInfo.strJobType}}</p>
                  <h6>开始招聘时间</h6>
                  <p>{{positionInfo.startDate}}</p> -->
                  <template slot="actions">
                    {{$t('position_management.date_published')}}：{{positionInfo.datePublished}}
                  </template>
                </a-card>
                <!-- <a-card :title="$t('position_management.position_nav')" class="navBar">
                </a-card> -->
              </div>
            </a-col>
          </a-row>
        </div>
      </a-tab-pane>
      <a-tab-pane key="2" :tab="`${$t('position_management.recommeded')} (${vendorRecommendCount})`" force-render>
        <div class="content">
          <candidate-table ref="cadidateTable" :searchPositionId="positionId" :searchType="searchType" @count="getCount" :noConditionIds="['positionName', 'positionId', 'enterpriseId']"/>
        </div>
      </a-tab-pane>
      <!-- <a-tab-pane key="3" :tab="`${$t('common.info')} (${notReadMessageCount || 0})`">
        <div class="content">
          <informations :more="more" :isSend="positionInfo.status !== 'PUBLISHING' || positionInfo.vendorPartnerStatus !== 'Y'" :enterpriseId="enterpriseId" :mesgCount="mesgCount" :messageList="messageList" @getMessageList="getMessageList" @sendMessage="sendMessage"/>
        </div>
      </a-tab-pane> -->
    </a-tabs>
    <analysising-modal :fileSize="fileSize" :positionInfo="positionInfo" :searchType="searchType" :callbackId="callbackId" v-if="showAnalysis" :currentPage="true" @setData="setData"/>
    <position-distribution :positionInfo="positionInfo" v-if="isdistributeResume" :visible="isdistributeResume" @close="(type) => distributeResume(false, {type})" :positionId="positionId" :sourcePid="sourcePid" :enterpriseId="enterpriseId"/>
    <position-rec-resume :positionInfo="positionInfo" v-if="showResume" :visible="showResume" @close="(data) => recommedResume(false, data)" :sourcePid="sourcePid" :positionId="positionId" :enterpriseId="enterpriseId"/>
  </div>
</template>

<script>
// import CountCard from '@/components/CountCard/CountCard', CountCard
import PositionRecResume from '@/components/position/PositionRecResume'
import CandidateTable from '@/components/resume/CandidateTable'
import analysisingModal from '@/components/analysisingModal/analysisingModal'
import PositionDistribution from '@/components/position/PositionDistribution'
import { cancelPosition, positionInfo } from '@/api/position'
import { messageList, sendToPositionTeamHr } from '@/api/resume'
import fieldGroups from '@/assets/config/detailTemp'
export default {
  name: 'functionType',
  components: { PositionRecResume, CandidateTable, PositionDistribution, analysisingModal },
  data () {
    return {
      menus: [
        { key: 'ALL', name: '全部', value: 0 },
        { key: 'success', name: '成功', value: 0 },
        { key: 'failed', name: '失败', value: 0 },
        { key: 'update', name: '已存在', value: 0 }
      ],
      pendingStatus: 'ALL',
      pendingTotal: 0,
      onlyOne: false,
      fileSize: 0,
      callbackId: '',
      successCount: 0,
      showAnalysis: false,
      isdistributeResume: false,
      searchType: '',
      enterpriseId: '',
      positionId: '',
      sourcePid: '',
      // tupuAtsPositionId: '',
      backPath: '',
      messageList: [],
      positionTemp: [{}],
      mesgCount: 0,
      vendorRecommendCount: 0,
      notReadMessageCount: 0,
      positionList: null,
      positionInfo: {},
      showResume: false,
      more: false,
      msgPageNum: 1,
      activeKey: '1',
      current: 1,
      max: 1
    }
  },
  watch: {
    $route: {
      handler (val, oldVal) {
        if (val.query.positionId !== oldVal.query.positionId) {
          this.positionId = val.query.positionId
          this.getPositionInfo()
          // this.getMessageList()
        }
      },
      deep: true
    }
  },
  computed: {
  },
  filters: {
  },
  created () {
    this.initData()
  },
  mounted () {
  },
  methods: {
    setSearch (item) {
      // this.$router.push({ query: { operator: item.key } })
      this.pendingStatus = item.key
    },
    distributeResume (flag, record) {
      if (record.type) {
        this.getPositionInfo()
      }
      this.isdistributeResume = flag
    },
    async closeVendors (e, vendorId) { // 猎头公司职位，删除分配给职位的猎头账号
      e.preventDefault()
      let params = {
        positionId: this.positionId,
        vendorIds: vendorId
      }
      let res = await cancelPosition(params)
      if (res.code === 200) {
        this.positionInfo.job_vendor_array = this.positionInfo.job_vendor_array.filter(item => item.id !== vendorId)
        this.$message.success(res.message)
      } else {
        this.$message.error(res.message)
      }
    },
    setData () {
      this.$refs.pendingTable.getPendingList()
      this.activeKey = '4'
      this.showAnalysis = false
    },
    pendingCount (data) {
      this.menus.forEach(item => {
        switch (item.key) {
          case 'ALL':
            item.value = data.totalCount
            break
          case 'success':
            item.value = data.successCount
            break
          case 'failed':
            item.value = data.failCount
            break
          case 'update':
            item.value = data.updateCount
            break
          default:
            break
        }
      })
      this.pendingTotal = data.totalCount
      if (this.onlyOne) {
        this.$refs.cadidateTable.getApplicationList()
      }
      this.onlyOne = true
    },
    getCount (count) {
      this.vendorRecommendCount = count
    },
    async initData () { // positionList
      this.activeKey = this.$route.query.activeKey || '1'
      this.searchType = this.$route.query.searchType
      this.enterpriseId = this.$route.query.enterpriseId
      this.positionId = this.$route.query.positionId
      this.sourcePid = this.$route.query.sourcePid
      // if (!this.positionId) {
      //   let res = await positionOne({ enterpriseId: this.enterpriseId, sourcePid: this.sourcePid })
      //   this.positionId = res.data.id.split('_')[0]
      // }
      // this.tupuAtsPositionId = this.$route.query.tupuAtsPositionId
      this.setPositionList()
      this.getPositionInfo()
      // this.getMessageList()
    },
    setPositionList () {
      let positionList = JSON.parse(sessionStorage.getItem('positionList') || '{"list": []}').list
      this.backPath = JSON.parse(sessionStorage.getItem('positionList') || '{}').backPath
      this.positionList = positionList
      positionList.some((item, index) => {
        if (item.positionId === this.positionId) {
          this.current = index + 1
        }
      })
      this.max = positionList.length
    },
    async getPositionInfo () {
      let params = {
        searchType: this.searchType,
        positionId: this.positionId
      }
      let res = await positionInfo(params)
      if (res.code === 200) {
        this.positionInfo = res.data
        this.positionTemp = fieldGroups
      } else {
        this.$message.error(res.message)
      }
    },
    backPage () {
      this.$router.push(this.backPath || '/myPosition')
    },
    pageChange (e) {
      this.current = e
      this.$router.push({
        query: {
          positionId: this.positionList[e - 1].positionId,
          searchType: this.searchType,
          enterpriseId: this.positionList[e - 1].enterpriseId,
          // tupuAtsPositionId: this.positionList[e - 1].tupuAtsPositionId,
          sourcePid: this.positionList[e - 1].sourcePid
        }
      })
    },
    callback (e) {
      this.activeKey = e
    },
    async getMessageList (type) { // 获取消息列表
      // 候选人：vendor_candidate
      // 职位：vendor_position
      this.more = true
      if (type) {
        this.msgPageNum = ++this.msgPageNum
      } else {
        this.msgPageNum = 1
      }
      let params = {
        enterpriseId: this.enterpriseId,
        category: 'message',
        visibility: 'vendor_position',
        // applicationId: this.id,
        positionId: this.positionId,
        startTime: '',
        endTime: '',
        pageSize: 15,
        pageNum: this.msgPageNum
      }
      let res = await messageList(params)
      if (res.code === 200) {
        // 处理表单数据并传递给页面
        if (type) {
          this.messageList = this.messageList.concat(res.data.items)
        } else {
          this.messageList = res.data.items
        }
        this.mesgCount = res.data.totalCount
        this.notReadMessageCount = res.data.notReadMessageCount
      } else {
        this.$message.error(res.message)
      }
      this.more = false
    },
    sendMessage (data) {
      if (data.user === 'HR') {
        this.setToPositionTeamHr(data.content)
      }
    },
    async setToPositionTeamHr (content) { // 发送消息给职位负责人
      let params = {
        type: 'position',
        message: {
          positionId: this.positionId,
          // applicationId: this.id,
          // name: this.resumeData.basic.name,
          // email: this.resumeData.basic.email,
          content
        }
      }
      let res = await sendToPositionTeamHr(params)
      if (res.code === 200) {
        // 处理表单数据并传递给页面
        this.$message.success(res.message)
        this.getMessageList()
      } else {
        this.$message.error(res.message)
      }
    },
    recommedResume (flag, record) {
      if (record.type === 'model') {
        this.showAnalysis = true
        this.fileSize = record.fileSize
      } else if (record.type) {
        this.getPositionInfo()
        this.$refs.cadidateTable.getApplicationList()
      }
      this.callbackId = record.callbackId || ''
      this.showResume = flag
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../styles/var.styl'
.distrube
  display flex
  .tagsList
    flex 1
  .addDistrub
    border-color #0166b6
    color #0166b6
.functionType
  .titleTop
    background #fff
    display flex
    align-items center
    justify-content flex-start
    padding  0 24px 12px
    h3
      margin-right 4px
      font-size 18px
      font-weight bold
    button
      margin-left 12px
  :global(.ant-tabs-nav-scroll)
    padding-left 24px
  :global(.ant-tabs-bar)
    background #fff
  .content
    padding 0 24px 12px
    // display flex
    // justify-content space-between
    .positionBasicInfo
      min-width 300px
      line-height 24px
      h6
        margin 0
        color cccGrey
      p
        margin-bottom 6px
    &.pt
      padding-top 12px
  :global(.CandidateTable)
    margin-top 12px
  .navBar
    margin-top 10px
    :global(.ant-card-body)
      text-align center
  .red
    color errorColor
  .under
    cursor pointer
    text-decoration underline
</style>
